﻿@using KPIS.GERP.Models.ViewModel
@using KPIS.GERP.Models.ICM
@model IEnumerable<PurchaseCheckAsset>

@{
    Layout = "~/Views/Shared/_EmptyLayout.cshtml";
    
    ViewBag.Title = "ข้อมูลพัสดุ";
    ViewBag.Descr = "ข้อมูลพัสดุ";

    //var PurchaseCheckAsseService = new KPIS.GERP.WEB.GIMS.PurchaseCheckAssetService.PurchaseCheckAssetServiceClient();
    
    
    
    var service = new KPIS.GERP.WEB.GIMS.PurchaseCheckAssetService.PurchaseCheckAssetServiceClient();
    var modelResourceItem = (dynamic)null;
    int countItem = 0;
    int StockTakingId = ViewBag.StockTakingId;
    string ResourceType = ViewBag.ResourceType;
 
    ViewBag.HeaderColumn = ResourceType == "วัสดุ" ? "หมายเลขวัสดุ" : "หมายเลขครุภัณฑ์";

    if(ResourceType == "วัสดุ")
    {
        modelResourceItem = service.SelectOfficeSuppliesLots(new OfficeSuppliesLot { RecordStatus = "!" + RecordStatus.DeletedStatus }
            , new[] { "MasResourceItem", "OfficeSupplyLotStatus" }).GroupBy(row => new
            {
                row.ResItemId,
                Code = row.MasResourceItem != null ? row.MasResourceItem.Code : "",
                Name = row.MasResourceItem != null ? row.MasResourceItem.Name : ""
            })
        .Select(g => new
        {
            ItemId = g.Key.ResItemId,
            ItemCode = g.Key.Code,
            ItemName = g.Key.Name,
            SumQty = g.Sum(x => x.Quantity)
        }).ToList();

        countItem = service.SelectOfficeSuppliesLots(new OfficeSuppliesLot { RecordStatus = "!" + RecordStatus.DeletedStatus }
        , new[] { "MasResourceItem" }).GroupBy(row => new { row.ResItemId })
        .Select(g => new
        {
            ItemId = g.Key.ResItemId
        }).Count();
        
    }
    else if (ResourceType == "ครุภัณฑ์")
    {
        modelResourceItem = service.SelectAssets(new Asset { RecordStatus = "!" + RecordStatus.DeletedStatus }
        , new[] { "MasResourceItem", "AssetStatus" }).GroupBy(row => new { row.ResItemId, row.MasResourceItem.Code, row.MasResourceItem.Name })
        .Select(g => new
        {
            ItemId = g.Key.ResItemId,
            ItemCode = g.Key.Code,
            ItemName = g.Key.Name,
            SumQty = g.Sum(x => x.Quantity)
        }).ToList();
        
        countItem = service.SelectAssets(new Asset { RecordStatus = "!" + RecordStatus.DeletedStatus }
        , new[] { "MasResourceItem" }).GroupBy(row => new { row.ResItemId })
        .Select(g => new
        {
            ItemId = g.Key.ResItemId
        }).Count();
    }
}

@using (Html.BeginForm("DeleteAllTakingItem", "PurchaseCheckAsset", FormMethod.Post, new { id = "form" }))
{
    @Html.AntiForgeryToken()
    
    <div class="row-fluid">    

        @Html.ValidationSummary(true, "", new { @class = "alert alert-error" })

        <div class="row-fluid">
            <div class="table-header span12">
                <div class="span10">
                @ViewBag.Title
                </div>
                <div class="btn-group pull-right">
                    @Html.Partial("_ExportButton")
                </div>
            </div>
        </div>

        <table id="table" class="table table-striped table-bordered table-hover" aria-describedby="table_info">
            <thead>
                <tr>
                    <th style="display:none;"></th>
                    <th style="width:100px;">@ViewBag.HeaderColumn</th>
                    <th>ยี่ห้อ ขนาด ลักษณะ
                    <th>จำนวนคงเหลือ</th>
                    <th>จำนวนคงเหลือตรวจนับ</th>
                    <th>สภาพการใช้งาน</th>
                    <th>ทำรายการ</th>
                </tr>
            </thead>

            <tbody>
                @for (int i=0; i<countItem; i++)               
                {
                    <tr>
                        <td style="display:none;"></td>

                        <td>
                            @modelResourceItem[i].ItemCode
                        </td>
                      
                        <td>
                            @modelResourceItem[i].ItemName
                        </td>

                        <td>
                            @modelResourceItem[i].SumQty
                        </td>

                        <td>
                            @{
                                var modelInventoryStockTakingItem = service.SelectInventoryStockTakingItem(new InventoryStockTakingItem { ResItemId = modelResourceItem[i].ItemId, RecordStatus = "!" + RecordStatus.DeletedStatus }, new[] {"Status"} ).FirstOrDefault();
                                string QtyReal = string.Empty;
                                string Status = string.Empty;
                                if (modelInventoryStockTakingItem != null)
                                {
                                    QtyReal = modelInventoryStockTakingItem.QuantityAmountReal.ToString();
                                    Status = modelInventoryStockTakingItem.Status.LookUpName;
                                }
                            }
                            @Html.Label(QtyReal)
                        </td>

                        <td>
                            @Html.Label(Status)
                        </td>
                        
                        <td>
                            @{
                            var modelStockTaking = service.SelectInventoryStockTaking(new InventoryStockTaking { StockTakingId = StockTakingId, RecordStatus = "!" + KPIS.GERP.Models.SYS.RecordStatus.DeletedStatus }
                                , new[] { "Warehouse", "JobSite", "MasResourceType" }).FirstOrDefault();

                            // ถ้ายังไม่มีการอนุมัติหรือส่งอนุมัติก็จะสามารถ เพิ่ม, ลบ, แก้ไข คณะกรรมการได้
                            var Expresstion = modelStockTaking.RecordStatus != KPIS.GERP.Models.SYS.RecordStatus.ApprovedStatus;
                            
                            }
                            @if(Expresstion)
                            {
                                if (modelInventoryStockTakingItem != null)
                                { 
                                   
                                    if (modelInventoryStockTakingItem.RecordStatus != KPIS.GERP.Models.SYS.RecordStatus.ApprovedStatus)
                                   {
                                       @Html.ActionLink("ตรวจนับ", "EditInventoryStockTakingItem", new { StockTakingId = ViewBag.StockTakingId, ResItemId = modelResourceItem[i].ItemId, SumAmountStock = modelResourceItem[i].SumQty }, new { @class = "btn btn-success btn-small", role="button" })
                                   }
                                   else
                                   {
                                       @Html.Raw("ตรวจนับแล้ว")                                       
                                   }
                                }
                                else
                                {
                                    @Html.ActionLink("ตรวจนับ", "CreateInventoryStockTakingItem", new { StockTakingId = ViewBag.StockTakingId, ResItemId = modelResourceItem[i].ItemId, SumAmountStock = modelResourceItem[i].SumQty }, new { @class = "btn btn-success btn-small", role="button" })
                                }
                            }
                        </td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
}

@section scripts {
    
	<script type="text/javascript">
		$(function () {
		    var oTable1 = $('#table').dataTable({
		        "aoColumns": [
                    { "bSortable": false },
                    null,
                    null,
                    null,
                    null,
                    null,
                    { "bSortable": false }
		        ]
		    });

		    $('table th input:checkbox').on('click', function () {
		        var that = this;
		        $(this).closest('table').find('tr > td:first-child input:checkbox')
				.each(function () {
					this.checked = that.checked;
					$(this).closest('tr').toggleClass('selected');
				});

		    });

		    $('[data-rel=tooltip]').tooltip();

		    $("#form").submit(function () {
		        var currentForm = this;
		        bootbox.confirm(function (result) {
		            var _result = result;
		            if (result) {
		                $.ajax({
		                    url: "DeleteAllBank",
		                    type: "post",
		                    dataType: "json",
		                    data: $(currentForm).serialize(),
		                    success: function (result) {
		                        if (result.success) {
		                            location.reload();
		                        } else {
		                        }
		                    }
		                });
		            }
		        })
		        return false;

		    });

		})

	</script>

}
